package com.dhecp.project.base.controller;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.layui.TreeDataModel;
import com.dhecp.core.layui.TreeDataUtil;
import com.dhecp.project.base.domain.BaseInfo;
import com.dhecp.project.base.service.BaseInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/base/baseinfo")
public class BaseInfoController extends BaseController {

    @Autowired
    private BaseInfoService baseInfoService;

    @Autowired
    private TreeDataUtil treeDataUtil;

    /**
     * 通过传入的条件查询基础数据列表
     * @param params
     * @return GridDataModel
     * @throws Exception
     * @author DH-Xxj
     */
//    @RequiresPermissions("system:base:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params) throws Exception{
        List<BaseInfo> list = new ArrayList<BaseInfo>();
        try {
        	if (params.get("vtype") == null || params.get("vtype") == ""){
        		params.put("moduleTable", "TB_baseinfo");
        	}else{
                String vtype = params.get("vtype").toString();
                String moduleTable = "TB_baseinfo_" + vtype;
                moduleTable = (moduleTable.toLowerCase().indexOf("tb_baseinfo_area") == -1 ? moduleTable : "tb_baseinfo_area");
        		params.put("moduleTable", moduleTable);
        	}
            list = baseInfoService.queryBaseInfoList(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());   
        }
        return getDataTable(list);
    }

    /**
     * 通过guid查询基础数据单条记录
     * @param guid
     * @return MessageModel
     * @throws Exception
     * @author DH-Xxj
     */
    @RequestMapping("/lineguid")
    @ResponseBody
    public AjaxResult baseInfolineJson(@RequestParam(required = false, defaultValue = "no", value = "guid") String guid) throws Exception{
        AjaxResult ajaxResult = new AjaxResult();
        try{
            BaseInfo baseInfo = baseInfoService.queryBaseInfoByGuid(guid);
            if (baseInfo == null){
                ajaxResult =  AjaxResult.error("没有符合条件的数据。");
            }else{
                ajaxResult = AjaxResult.success("查询成功。", baseInfo);
            }
        }catch(Exception e){
            ajaxResult =  AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 通过基础数据分项（vtype）、基础数据编码（vid）查询基础数据单条记录
     * @param vtype
     * @param vid
     * @return MessageModel
     * @throws Exception
     * @author DH-Xxj
     */
    @RequestMapping("/linevid")
    @ResponseBody
    public AjaxResult baseInfolineJson(@RequestParam(required = false, defaultValue = "no", value = "vtype") String vtype,
                                         @RequestParam(required = false, defaultValue = "no", value = "vid") String vid) throws Exception{
        AjaxResult ajaxResult = new AjaxResult();
        try{
            BaseInfo baseInfo = baseInfoService.queryBaseInfoByVtypeAndVid(vtype,vid);
            if (baseInfo == null){
                ajaxResult =  AjaxResult.error("没有符合条件的数据。");
            }else{
                ajaxResult = AjaxResult.success("查询成功。", baseInfo);
            }
        }catch(Exception e){
            ajaxResult =  AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }


    /**
     * 通过传入的条件查询基础数据树
     * @param params
     * @return TreeDataModel
     * @throws Exception
     * @author DH-Xxj
     */
//    @RequiresPermissions("system:base:list")
    @RequestMapping("/listtreejson")
    @ResponseBody
    public Object baseInfoTreeJson(@RequestParam Map<String, Object> params) throws Exception{
        if (params.get("rootvtype") == null || params.get("rootvtype") == ""){
            //根节点类型，若未传入默认为区域"area"
            params.put("rootvtype", "area1");
        }
        List<TreeDataModel> treeDataModelList = new ArrayList<TreeDataModel>();
        try{
            treeDataModelList = baseInfoService.queryBaseInfoTree(params);
            treeDataModelList = treeDataUtil.dealTreeDataModel(treeDataModelList,treeDataModelList.get(0).getMaxlevels());
        }catch(Exception e){
            return treeDataModelList;
        }
        return treeDataModelList;
    }

    /**
     * 新增基础数据
     * @param baseInfoItems 对应实体类的json格式字符串，示例：[{"key11":"value11","key12":"value12"},{"key21":"value21","key22":"value22"}]
     * @return MessageModel
     * @author DH-Xxj
     */
    @RequiresPermissions("system:base:add")
    @Log(title = "基础数据新增", businessType = BusinessType.INSERT)
    @PostMapping("/addpost")
    @ResponseBody
    public AjaxResult baseInfoAddPost(@RequestParam("baseInfoItems") String baseInfoItems) {
        AjaxResult ajaxResult = new AjaxResult();
        if (StringUtils.isEmpty(baseInfoItems)) {
            return  AjaxResult.error("数据提交失败，必填参数有误！");
        }
        try {
            ajaxResult= baseInfoService.insertBaseInfo(baseInfoItems);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 删除基础数据
     * @param baseInfoItems 对应实体类的json格式字符串，示例：[{"key11":"value11","key12":"value12"},{"key21":"value21","key22":"value22"}]
     * @return MessageModel
     * @author DH-Xxj
     */
    @RequiresPermissions("system:base:delete")
    @Log(title = "基础数据删除", businessType = BusinessType.DELETE)
    @PostMapping("/deletepost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult baseInfoDeletePost(@RequestParam("baseInfoItems") String baseInfoItems) {
        AjaxResult ajaxResult = new AjaxResult();
        if (StringUtils.isEmpty(baseInfoItems)) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }
        try {
            ajaxResult= baseInfoService.deleteBaseInfo(baseInfoItems);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 修改基础数据
     * @param baseInfoItems 对应实体类的json格式字符串，示例：[{"key11":"value11","key12":"value12"},{"key21":"value21","key22":"value22"}]
     * @return MessageModel
     * @author DH-Xxj
     */
    @RequiresPermissions("system:base:edit")
    @Log(title = "基础数据修改", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult baseInfoEditPost(@RequestParam("baseInfoItems") String baseInfoItems) {
        AjaxResult ajaxResult = new AjaxResult();
        if (StringUtils.isEmpty(baseInfoItems)) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }
        try {
            ajaxResult= baseInfoService.updateBaseInfo(baseInfoItems);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 修改基础数据默认标志
     * @param baseInfoItems 对应实体类的json格式字符串，示例：[{"key11":"value11","key12":"value12"},{"key21":"value21","key22":"value22"}]
     * @return MessageModel
     * @author DH-Xxj
     */
    @RequiresPermissions("system:base:edit")
    @Log(title = "基础数据修改默认标志", businessType = BusinessType.UPDATE)
    @PostMapping("/editbdefaultpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult baseInfoBdefaultEditPost(@RequestParam("baseInfoItems") String baseInfoItems) {
        AjaxResult ajaxResult = new AjaxResult();
        if (StringUtils.isEmpty(baseInfoItems)) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }
        try {
            ajaxResult= baseInfoService.updateBaseInfoBdefault(baseInfoItems);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 修改基础数据禁用标志
     * @param baseInfoItems 对应实体类的json格式字符串，示例：[{"key11":"value11","key12":"value12"},{"key21":"value21","key22":"value22"}]
     * @return MessageModel
     * @author DH-Xxj
     */
    @RequiresPermissions("system:base:edit")
    @Log(title = "基础数据修改禁用标志", businessType = BusinessType.UPDATE)
    @PostMapping("/editbdeletedpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult baseInfoBdeletedEditPost(@RequestParam("baseInfoItems") String baseInfoItems) {
        AjaxResult ajaxResult = new AjaxResult();
        if (StringUtils.isEmpty(baseInfoItems)) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }
        try {
            ajaxResult= baseInfoService.updateBaseInfoBdeleted(baseInfoItems);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
}
